from sqlalchemy import Table, Column, String, ForeignKey
from sqlalchemy import MetaData, UniqueConstraint, Integer, Sequence


def get():
    metadata = MetaData()

    # Tenants enum
    #
    # name - tenant name
    Table('tenants', metadata,
          Column('id', String, primary_key=True),
          Column('name', String, nullable=False))

    # Username tenants association
    #
    # login - login name of user
    # tenant_fk - pointer to tenant
    Table('users_tenants', metadata,
          Column('login', String, nullable=False),
          Column('tenant_fk', String,
                              ForeignKey("tenants.id"),
                              nullable=False))

    # Tenant specific parameters
    #
    # tenant_fk - pointer to tenant
    # param_name - does product have serial number
    # param_value - pointer to unit of measure
    # scope - parameter visibility - public/private. Only public parameters can
    #         be queried externally
    Table('tenants_params', metadata,
          Column('tenant_fk', String,
                              ForeignKey("tenants.id"),
                              nullable=False),
          Column('param_name', String, nullable=False),
          Column('param_value', String, nullable=False),
          Column('scope', String, nullable=False),
          UniqueConstraint('tenant_fk', 'param_name', 'scope'))

    # URLs of physical datasources
    #
    # url - physical datasource url
    Table('ds', metadata,
          Column('id', Integer, Sequence('DS_ID_SEQ'), primary_key=True),
          Column('url', String, nullable=False))

    return metadata
